//index.js
//获取应用实例
const app = getApp()
const backgroundAudioManager = wx.getBackgroundAudioManager()
//个位数补零
function supple(num) {
  return num < 10 ? '0' + num : num
}

Page({
  data: {
    poster: 'http://10.1.6.249:1234/icon-Listen.png',
    name: '最美的光',
    author: '尚琪祺',
    src: 'http://10.1.6.249:1234/%E5%B0%9A%E7%90%AA%E7%A5%BA%20-%20%E6%9C%80%E7%BE%8E%E7%9A%84%E5%85%89.mp3',
    state: 0,
    ready: false,
    lrc: 'http://10.1.6.249:1234/%E5%B0%9A%E7%90%AA%E7%A5%BA%20-%20%E6%9C%80%E7%BE%8E%E7%9A%84%E5%85%89.lrc',
    lrcTimeArr:[],
    lrcTextArr:[],
    cur: 0,
    time:'10:10',
    startCount: false,
    countEnd: false
  },
  onLoad: function () {
    console.log('页面加载完成')
  },
  onReady: function (e) {
    console.log('页面初次渲染完成')
    this.setData({
      ready: true
    })
    let that = this
    wx.request({
      url: this.data.lrc,
      success: function (res) {
        let lrcObj = that.handleLrc(res.data)
        let lrcTimeArr = []
        let lrcTextArr = []
        for (let item in lrcObj){
          lrcTimeArr.push(item)
          lrcTextArr.push(lrcObj[item])
        }
        that.setData({
          lrcTimeArr, lrcTextArr
        })
      }
    })
  },
  onShow: function (e) {
    if (this.data.ready) {
      let thisState = backgroundAudioManager.paused ? 0 : 1
      this.setData({
        state: thisState
      })
      if(thisState == 0){
        this.setData({
          startCount: false
        })
      }
    }
  },
  onHide: function (e) {
  },
  //播放音乐
  play: function () {
    let that = this
    this.setData({
      state: 1
    })
    backgroundAudioManager.title = this.data.name
    backgroundAudioManager.epname = this.data.name
    backgroundAudioManager.singer = this.data.author
    backgroundAudioManager.coverImgUrl = this.data.poster
    backgroundAudioManager.src = this.data.src
    //播放进度更新事件
    backgroundAudioManager.onTimeUpdate(()=>{
      let currentTime = backgroundAudioManager.currentTime
      if (currentTime) {
        that.scrollLrc(currentTime)
      }
    })
    //自然播放结束事件
    backgroundAudioManager.onEnded(() => {
      this.setData({
        state: 0
      })
    })
    //倒计时
    if (!this.data.countEnd) {
      this.setData({
        startCount: true
      })
      this.countDown(this)
    }
  },
  //暂停音乐
  pause: function () {
    this.setData({
      state: 0,
      startCount: false
    })
    backgroundAudioManager.pause()
  },
  //滚动歌词
  scrollLrc: function (currentTime) {
    let that = this
    let second = parseInt(currentTime)
    let lrcTimeArr = this.data.lrcTimeArr
    let index = lrcTimeArr.indexOf(String(second))
    if (index > -1){
      that.setData({
        cur: index
      })
    }
  },
  //处理歌词
  handleLrc: function (lrc) {
    var lyrics = lrc.split("\n");
    var lrcObj = {};
    for (var i = 0; i < lyrics.length; i++) {
      var lyric = decodeURIComponent(lyrics[i]);
      var timeReg = /\[\d*:\d*((\.|\:)\d*)*\]/g;
      var timeRegExpArr = lyric.match(timeReg);
      if (!timeRegExpArr) continue;
      var clause = lyric.replace(timeReg, '');
      for (var k = 0, h = timeRegExpArr.length; k < h; k++) {
        var t = timeRegExpArr[k];
        var min = Number(String(t.match(/\[\d*/i)).slice(1)),
          sec = Number(String(t.match(/\:\d*/i)).slice(1));
        var time = min * 60 + sec;
        lrcObj[time] = clause;
      }
    }
    return lrcObj;
  },  
  //倒计时
  countDown: function (that) {
    if (that.data.startCount) {
      let now = that.data.time
      let minute = parseInt(now.split(':')[0])
      let second = parseInt(now.split(':')[1])
      if (second == 0 && minute == 0) {
        console.log('倒计时结束')
        this.setData({
          state: 0,
          startCount: false,
          countEnd: true
        })
        wx.pauseBackgroundAudio()
        return false
      }
      if (second > 0) {
        second--
      }
      else if (second == 0 && minute > 0){
        second = 59
        minute--
      }
      that.setData({
        time: supple(minute) + ':' + supple(second)
      })
      let timeOut = setTimeout(function(){        
        that.countDown(that)
      },1000)
    }
  }
})
